import { pathOr } from 'ramda';

import { VUL_LEVEL_MAP } from '../../../../common/constants';
import { warnEventId } from '../../../../common/warnId';

export default function ssl({ target, taskSettings, response }) {
	const results = pathOr([], ['sslcheck', 'results'], response);
	const data = pathOr([], ['sslcheck', 'data'], response);
	const pluginCategoryMap = (
		taskSettings?.collect?.pluginCategories || []
	).reduce(
		// eslint-disable-next-line no-sequences
		(obj, x) => ((obj[x] = true), obj),
		{}
	);

	return {
		addition: data,
		warnings: results.reduce((arr, x) => {
			const {
				description,
				category,
				impact,
				recommendation,
				risk,
				name,
				display_name: title,
			} = x;
			// 过滤没有开启监测的类别
			if (pluginCategoryMap[category]) {
				arr.push({
					target,
					affects: target,
					title,
					description,
					category,
					impact,
					recommendation,
					detail: x,
					id: warnEventId({
						affects: target,
						taskType: 'ssl',
						id: name,
					}),
					level: VUL_LEVEL_MAP[risk],
					levelName: x.risk,
					foundAt: response.found_at,
				});
			}

			return arr;
		}, []),
	};
}
